package com.fr.data.impl;

import com.fr.base.XMLable;
import com.fr.base.core.BaseCoreUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.AbstractTableData;
import com.fr.data.core.DataXMLUtils;
import com.fr.util.Utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class TextTableData extends AbstractTableData
  implements XMLable
{
  private DirectoryConnection directoryConnection;
  private String delimiter = null;
  private boolean isIgnoreOneMoreDelimiter = true;
  private String filePath = null;
  private List columnNameList = new ArrayList();
  private int columnCount = 0;
  private List rowValueList = new ArrayList();
  private boolean isInitialed = false;
  private boolean needColumnName = false;

  public DirectoryConnection getDirectoryConnection()
  {
    return this.directoryConnection;
  }

  public void setDirectoryConnection(DirectoryConnection paramDirectoryConnection)
  {
    this.directoryConnection = paramDirectoryConnection;
  }

  public String getFilePath()
  {
    return this.filePath;
  }

  public void setFilePath(String paramString)
  {
    this.filePath = paramString;
  }

  public boolean needColumnName()
  {
    return this.needColumnName;
  }

  public void setNeedColumnName(boolean paramBoolean)
  {
    this.needColumnName = paramBoolean;
  }

  public String getDelimiter()
  {
    return this.delimiter;
  }

  public void setDelimiter(String paramString)
  {
    this.delimiter = paramString;
  }

  public boolean isIgnoreOneMoreDelimiter()
  {
    return this.isIgnoreOneMoreDelimiter;
  }

  public void setIgnoreOneMoreDelimiter(boolean paramBoolean)
  {
    this.isIgnoreOneMoreDelimiter = paramBoolean;
  }

  public int getColumnCount()
  {
    loadColumnName();
    return this.columnNameList.size();
  }

  public String getColumnName(int paramInt)
  {
    loadColumnName();
    Object localObject = (paramInt < this.columnNameList.size()) ? this.columnNameList.get(paramInt).toString() : null;
    return localObject;
  }

  public int getRowCount()
  {
    initial();
    return this.rowValueList.size();
  }

  public Object getValueAt(int paramInt1, int paramInt2)
  {
    initial();
    List localList = (List)this.rowValueList.get(paramInt1);
    return ((paramInt2 < localList.size()) ? localList.get(paramInt2) : null);
  }

  private void loadColumnName()
  {
    String str;
    Object localObject;
    File localFile = new File(this.directoryConnection.getLocation());
    if (!(localFile.isDirectory()))
      return;
    this.columnNameList.clear();
    if (this.needColumnName)
    {
      int i;
      try
      {
        BufferedReader localBufferedReader1 = new BufferedReader(new InputStreamReader(new FileInputStream(BaseCoreUtils.pathJoin(new String[] { localFile.getPath(), this.filePath }))));
        str = null;
        do
        {
          if ((str = localBufferedReader1.readLine()) == null)
            break label154;
          localObject = Utils.splitString(str, this.delimiter);
          str = str.trim();
        }
        while ((str == null) || (str.length() <= 0));
        label154: label303: for (i = 0; i < localObject.length; ++i)
          this.columnNameList.add(localObject[i].trim());
      }
      catch (Exception localException1)
      {
        localException1.printStackTrace();
      }
    }
    else
    {
      int j;
      try
      {
        String[] arrayOfString;
        BufferedReader localBufferedReader2 = new BufferedReader(new InputStreamReader(new FileInputStream(BaseCoreUtils.pathJoin(new String[] { localFile.getPath(), this.filePath }))));
        str = null;
        do
        {
          if ((str = localBufferedReader2.readLine()) == null)
            break label303;
          localObject = new ArrayList();
          arrayOfString = Utils.splitString(str, this.delimiter);
          str = str.trim();
        }
        while ((str == null) || (str.length() <= 0));
        for (j = 0; j < arrayOfString.length; ++j)
          this.columnNameList.add("#" + (j + 1));
      }
      catch (Exception localException2)
      {
        localException2.printStackTrace();
      }
    }
  }

  private void initial()
  {
    if (this.isInitialed)
      return;
    File localFile = new File(this.directoryConnection.getLocation());
    if (!(localFile.isDirectory()))
      return;
    this.rowValueList.clear();
    try
    {
      BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(BaseCoreUtils.pathJoin(new String[] { localFile.getPath(), this.filePath }))));
      String str = null;
      while ((str = localBufferedReader.readLine()) != null)
      {
        ArrayList localArrayList = new ArrayList();
        str = str.trim();
        String[] arrayOfString = Utils.splitString(str, this.delimiter);
        if (isIgnoreOneMoreDelimiter())
        {
          j = 0;
          while (true)
          {
            if (j >= arrayOfString.length)
              break label204;
            if (!(arrayOfString[j].equals("")))
              localArrayList.add(arrayOfString[j].trim());
            ++j;
          }
        }
        for (int j = 0; j < arrayOfString.length; ++j)
          localArrayList.add(arrayOfString[j].trim());
        label204: this.rowValueList.add(localArrayList);
      }
      localBufferedReader.close();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    this.columnCount = this.columnNameList.size();
    if (!(this.needColumnName))
    {
      this.columnNameList.clear();
      int i = 0;
      while (true)
      {
        if (i >= 4)
          break label315;
        this.columnNameList.add("#" + (i + 1));
        ++i;
      }
    }
    this.rowValueList.remove(0);
    label315: this.isInitialed = true;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isChildNode())
    {
      String str2 = paramXMLableReader.getTagName();
      if ("TextTableDataAttr".equals(str2))
      {
        String str1;
        if ((str1 = paramXMLableReader.getAttr("delimiter")) != null)
          if (str1.equals("TTT"))
            setDelimiter("\t");
          else
            setDelimiter(str1);
        if ((str1 = paramXMLableReader.getAttr("filePath")) != null)
          this.filePath = str1;
        if ((str1 = paramXMLableReader.getAttr("needColumnName")) != null)
          if (str1.equals("true"))
            setNeedColumnName(true);
          else
            setNeedColumnName(false);
        if ((str1 = paramXMLableReader.getAttr("isIgnoreOneMoreDelimiter")) != null)
          if (str1.equals("true"))
            setIgnoreOneMoreDelimiter(true);
          else
            setIgnoreOneMoreDelimiter(false);
      }
      else if ("Connection".equals(str2))
      {
        Connection localConnection = DataXMLUtils.readXMLConnection(paramXMLableReader);
        if (localConnection instanceof DirectoryConnection)
          this.directoryConnection = ((DirectoryConnection)localConnection);
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    String str = this.delimiter;
    if (str.equals("\t"))
      str = "TTT";
    paramXMLPrintWriter.startTAG("TextTableDataAttr").attr("delimiter", str).attr("needColumnName", needColumnName()).attr("isIgnoreOneMoreDelimiter", isIgnoreOneMoreDelimiter()).attr("filePath", getFilePath()).end();
    if (this.directoryConnection != null)
      DataXMLUtils.writeXMLConnection(paramXMLPrintWriter, this.directoryConnection);
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof TextTableData) && (this.isIgnoreOneMoreDelimiter == ((TextTableData)paramObject).isIgnoreOneMoreDelimiter) && (this.needColumnName == ((TextTableData)paramObject).needColumnName) && (ComparatorUtils.equals(this.delimiter, ((TextTableData)paramObject).delimiter)) && (ComparatorUtils.equals(this.filePath, ((TextTableData)paramObject).filePath)) && (ComparatorUtils.equals(this.directoryConnection, ((TextTableData)paramObject).directoryConnection)));
  }
}